/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-4
 * @Time  : 上午11:32
 * @brief :
 */
(function () {
    var Dialog = require('common:widget/ui/dialog/dialog.js').Dialog,
        dialog = new Dialog();

    P.createPage({
        _data:{},
        _create:function(){
            var $page = $('.page-bankcard'),
                $addBank = $page.find('.add-bank'),
                $myBanks = $page.find('.my-banks');

            this.data({
                '$page':$page,
                '$addBank':$addBank,
                '$myBanks':$myBanks
            });
        },

        _init:function(){
            var $addBank = this.data('$addBank'),
                $myBanks = this.data('$myBanks');

            $myBanks.find('.btn-del').live('click',function(){
                var me = this,
                    id = $(this).attr('data-id');
                $.post('/bank/delbankcard',{
                    bankcard_id:id
                },function(ret,status){
                    if(ret.error_no == 0){
                       $(me).parent('.bank-block').remove();
                        dialog.show('银行卡删除成功');
                    }else{
                        dialog.show('删除银行卡错误，请稍后重试~');
                    }
                },'json');
            });

            $addBank.find('.btn-add').click(function(){
                var bankcardNumber =  $addBank.find('.bank-number').val(),
                    bankName =  $addBank.find('.self').attr('data-name'),
                    bankType = $addBank.find('.self').attr('data-type');

                if(!bankType){
                    dialog.show('未选择银行卡!');
                    return;
                }

                if(!/^\d{15,20}$/.test(bankcardNumber)){
                    dialog.show('银行卡号输入错误!');
                    return;
                }

                $.post('/bank/bindbankcard',{
                    bankcard_number:bankcardNumber,
                    bank_type:bankType
                },function(ret,data){
                    if(ret.error_no == 0){
                        alert("银行卡添加成功");
                        location.reload();
                    }else{
                        dialog.show('银行卡添加失败');
                    }
//                        $myBanks.find('.clear').remove();
//                        $myBanks.append('' +
//                            ' <li class="bank-block">' +
//                            '<div class="bank-type">'+bankName+'</div>' +
//                            '<div class="bank-number">'+bankcardNumber+'</div>' +
//                            '<a class="button btn-del" data-id=""></a>' +
//                            '</li>' +
//                            '<div class="clear"></div>' +
//                            '');
                },'json');
            });

            $addBank.find('.drop-menu li').click(function(e){
                e.stopPropagation();
                $addBank.find('.drop-menu').slideUp();
                $addBank.find('.self').text($(this).attr('data-name'));
                $addBank.find('.self').attr('data-name',$(this).attr('data-name'));
                $addBank.find('.self').attr('data-type',$(this).attr('data-type'));
            });

            $addBank.find('.self').click(function(e){
                e.stopPropagation();
                $addBank.find('.drop-menu').slideDown();
            });

            $('body').click(function(){
                $addBank.find('.drop-menu').slideUp();
            });

        }
    });


})();